package forms;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class NameSubmitForm extends ActionForm {
	private String firstName = null;
	private String lastName = null;
	private String middleInit = null;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getMiddleInit() {
		return middleInit;
	}
	public void setMiddleInit(String middleInit) {
		this.middleInit = middleInit;
	}
	
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
		ActionErrors errors = new ActionErrors();
		Pattern regEx = Pattern.compile("\\d");
		Matcher matcher = regEx.matcher(firstName);
		
		if ((firstName == null) || (firstName.length() < 1)||matcher.find())
			errors.add("firstNameMsgTagl", new ActionMessage("errors.required", "firstName"));
		matcher.reset(lastName);
		if ((lastName == null) || (lastName.length() < 1)||matcher.find())
			errors.add("lastNameMsgTagl", new ActionMessage("errors.required", "lastName"));
		matcher.reset(middleInit);
		if ((middleInit == null) || (middleInit.length() < 1)||matcher.find())
			errors.add("middleInitMsgTagl", new ActionMessage("errors.required", "middleInit"));
		
		
		return errors;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request){
		setFirstName(null);
		setLastName(null);
		setMiddleInit(null);
		
	}
}
